package cn.test.future.cf;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 任务交互测试
 */
@Slf4j
public class CompletableFutureTest6 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFutureTest6 test6 = new CompletableFutureTest6();
        test6.applyToEither();
        System.out.println("-------------");
        test6.acceptEither();
        System.out.println("-------------");
        test6.runAfterEither();
        System.out.println("-------------");
        test6.runAfterBoth();
        System.out.println("-------------");
        test6.anyOf();
        System.out.println("-------------");
        test6.allOf();
    }

    public void allOf() {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("future1 完成");
                return "future1";
            }
        });

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                System.out.println("future2 完成");
                return "future2";
            }
        });

        CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2);
        combinedFuture.join();

        System.out.println("future1 结果：" + future1.isDone() + ", future2 结果：" + future2.isDone());
    }

    public void anyOf() {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                int num = new Random().nextInt(10);
                try {
                    TimeUnit.SECONDS.sleep(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "hello";
            }
        });

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                int num = new Random().nextInt(10);
                try {
                    TimeUnit.SECONDS.sleep(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "world";
            }
        });

        CompletableFuture<Object> result = CompletableFuture.anyOf(future1, future2);
        System.out.println("结果：" + result.join());
    }

    public void runAfterBoth() {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("任务1：start");
                try {
                    TimeUnit.SECONDS.sleep(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务1：end");
                return num;
            }
        });

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("任务2：start");
                try {
                    TimeUnit.SECONDS.sleep(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务2：end");
                return num;
            }
        });

        future1.runAfterBoth(future2, new Runnable() {
            @Override
            public void run() {
                System.out.println("两个任务都执行完毕了");
            }
        }).join();
    }

    public void runAfterEither() {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("任务1：start");
                try {
                    TimeUnit.SECONDS.sleep(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务1：end");
                return num;
            }
        });

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("任务2：start");
                try {
                    TimeUnit.SECONDS.sleep(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务2：end");
                return num;
            }
        });

        future1.runAfterEither(future2, new Runnable() {
            @Override
            public void run() {
                System.out.println("已经有一个任务执行完毕了");
            }
        }).join();
    }

    public void applyToEither() {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("任务1：start");
                try {
                    TimeUnit.SECONDS.sleep(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务1：end");
                return num;
            }
        });

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("任务2：start");
                try {
                    TimeUnit.SECONDS.sleep(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务2：end");
                return num;
            }
        });

        System.out.println(future1.applyToEither(future2, new Function<Integer, Integer>() {
            @Override
            public Integer apply(Integer integer) {
                System.out.println("最快执行的任务：" + integer);
                return integer + 1;
            }
        }).join());
    }


    public void acceptEither() {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("任务1：start");
                try {
                    TimeUnit.SECONDS.sleep(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务1：end");
                return num;
            }
        });

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("任务2：start");
                try {
                    TimeUnit.SECONDS.sleep(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务2：end");
                return num;
            }
        });

        future1.acceptEither(future2, new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                System.out.println("先获取的结果是：" + integer);
            }
        }).join();
    }
}
